package model

import (
	"time"

	"golang.org/x/crypto/bcrypt"
	"gorm.io/gorm"
)

// User 用户模型
type User struct {
	ID        uint           `gorm:"primarykey" json:"id"`
	CreatedAt time.Time      `json:"created_at"`
	UpdatedAt time.Time      `json:"updated_at"`
	DeletedAt gorm.DeletedAt `gorm:"index" json:"-"`
	Username  string         `gorm:"size:64;not null;uniqueIndex:idx_username;comment:用户名" json:"username"`
	Password  string         `gorm:"size:128;not null;comment:密码" json:"-"`
	RealName  string         `gorm:"size:64;comment:真实姓名" json:"real_name"`
	Avatar    string         `gorm:"size:255;comment:头像" json:"avatar"`
	Mobile    string         `gorm:"size:11;comment:手机号" json:"mobile"`
	Email     string         `gorm:"size:128;comment:邮箱" json:"email"`
	Status    int            `gorm:"default:1;comment:状态(1:启用,2:禁用)" json:"status"`
	LastLogin time.Time      `gorm:"comment:最后登录时间" json:"last_login"`
	DeptID    uint           `gorm:"comment:部门ID" json:"dept_id"`
	Remark    string         `gorm:"size:255;comment:备注" json:"remark"`
	// 关联
	Roles []Role       `gorm:"many2many:go_user_role;" json:"roles,omitempty"`
	Depts []Department `gorm:"many2many:go_user_dept;" json:"depts,omitempty"`
}

// TableName 设置表名
func (User) TableName() string {
	return "go_user"
}

// CheckPassword 校验密码
func (u *User) CheckPassword(password string) bool {
	err := bcrypt.CompareHashAndPassword([]byte(u.Password), []byte(password))
	return err == nil
}

// EncryptPassword 加密密码
func EncryptPassword(password string) (string, error) {
	hash, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	return string(hash), nil
}

// LoginRequest 登录请求
type LoginRequest struct {
	Username string `form:"username" json:"username" binding:"required,min=5,max=30"`
	Password string `form:"password" json:"password" binding:"required,min=6,max=30"`
}

// LoginResponse 登录响应
type LoginResponse struct {
	Token        string    `json:"token"`
	TokenType    string    `json:"token_type"`
	ExpiresAt    time.Time `json:"expires_at"`
	RefreshToken string    `json:"refresh_token,omitempty"`
}

// UserSearchParams 用户搜索参数
type UserSearchParams struct {
	Username string `form:"username" json:"username"`
	RealName string `form:"real_name" json:"real_name"`
	Mobile   string `form:"mobile" json:"mobile"`
	Status   int    `form:"status" json:"status"`
	RoleID   uint   `form:"role_id" json:"role_id"`
	DeptID   uint   `form:"dept_id" json:"dept_id"`
}

// CreateUserRequest 创建用户请求
type CreateUserRequest struct {
	Username string `json:"username" binding:"required,min=5,max=30"`
	Password string `json:"password" binding:"required,min=6,max=30"`
	RealName string `json:"real_name" binding:"omitempty,max=64"`
	Avatar   string `json:"avatar" binding:"omitempty,max=255,url"`
	Mobile   string `json:"mobile" binding:"omitempty,len=11"`
	Email    string `json:"email" binding:"omitempty,max=128,email"`
	Status   int    `json:"status" binding:"omitempty,oneof=1 2"`
	RoleIDs  []uint `json:"role_ids" binding:"omitempty"`
	DeptIDs  []uint `json:"dept_ids" binding:"omitempty"`
}

// UpdateUserRequest 更新用户请求
type UpdateUserRequest struct {
	RealName string `json:"real_name" binding:"omitempty,max=64"`
	Avatar   string `json:"avatar" binding:"omitempty,max=255,url"`
	Mobile   string `json:"mobile" binding:"omitempty,len=11"`
	Email    string `json:"email" binding:"omitempty,max=128,email"`
	Status   int    `json:"status" binding:"omitempty,oneof=1 2"`
	RoleIDs  []uint `json:"role_ids" binding:"omitempty"`
	DeptIDs  []uint `json:"dept_ids" binding:"omitempty"`
}

// ResetPasswordRequest 重置密码请求
type ResetPasswordRequest struct {
	OldPassword string `json:"old_password" binding:"required,min=6,max=30"`
	NewPassword string `json:"new_password" binding:"required,min=6,max=30"`
}

// UserProfileResponse 用户详情响应
type UserProfileResponse struct {
	ID       uint   `json:"id"`
	Username string `json:"username"`
	RealName string `json:"real_name"`
	Avatar   string `json:"avatar"`
	Mobile   string `json:"mobile"`
	Email    string `json:"email"`
	Status   int    `json:"status"`
	Roles    []Role `json:"roles,omitempty"`
}

// RegisterRequest 用户注册请求
type RegisterRequest struct {
	Username string `json:"username" binding:"required,min=4,max=50" example:"johndoe"`
	Password string `json:"password" binding:"required,min=6,max=50" example:"123456"`
	RealName string `json:"real_name" binding:"required,max=50" example:"John Doe"`
	Mobile   string `json:"mobile" binding:"required,max=20" example:"13800138000"`
	Email    string `json:"email" binding:"required,email,max=100" example:"johndoe@example.com"`
	RoleIDs  []uint `json:"role_ids" binding:"required,min=1"`
	DeptID   uint   `json:"dept_id"`
	Status   int    `json:"status" binding:"oneof=1 2" example:"1"`
	Remark   string `json:"remark" binding:"max=255" example:"Test user"`
	DeptIDs  []uint `json:"dept_ids" binding:"omitempty"`
}
